/* 
 * Created on 2007-4-22
 * Last modified on 2007-4-22
 * Powered by YeQiangWei.com
 */
package com.yeqiangwei.commons.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import com.yeqiangwei.commons.net.HttpWrapper;
import com.yeqiangwei.commons.util.ValidatorHelper;

public class RemoteFile {
	
	/** 
	* 采集加缩放图片 
	* @param fileUrl 文件url路径 
	* @param filePathAndName 预创建到本地的文件的绝对路径含文件名已经扩展名 
	* @param width 宽度
	* @param height 高度
	* @throws Exception 
	*/ 
	public static void getImage(String fileUrl, String filePathAndName) throws Exception 
	{ 
		String head = fileUrl.substring(0,fileUrl.indexOf(":"));
		fileUrl = fileUrl.replaceAll(head,head.toLowerCase());
		if(fileUrl.indexOf("http")==-1
				 && fileUrl.indexOf("https")==-1
				 && fileUrl.indexOf("ftp")==-1
				 && fileUrl.indexOf("rtsp")==-1
				 && fileUrl.indexOf("mms")==-1
				
		){
			return ;
		}
		//读取内容    
		InputStream ins = HttpWrapper.getStream(fileUrl);
		if(ValidatorHelper.isEmpty(ins)){
			throw new Exception("InputStream is null");
		}
		BufferedInputStream bis = new BufferedInputStream(ins); 
		BufferedOutputStream bos = null; 
		File f = new File(filePathAndName); 
		bos = new BufferedOutputStream(new FileOutputStream(f));; 
		byte[] b = new byte[1024]; 
		int i = 0; 
		while((i = bis.read(b))>0) 
	    { 
			bos.write(b, 0, i); 
	    }  
		bos.flush(); 
		bis.close(); 
		ins.close();
		return ; 
	} 
}
